<?php
class Shop_Model_Kinrin extends Shop_Model_Base{

	/**
	 * 表示用変換
	 * @param $tenpoList
	 * @param $tenpoimageurl
	 * @param $linkurl
	 * @param $params
	 * 
	 * @return 変換後リスト
	 */
	public function convertList($tenpoList, $tenpoimageurl, $linkurl, $params)
	{
		$dispData = array();
		
    	foreach ($tenpoList as $data) {

    		// 店舗名変換
    		$data['M05_TENPO_NAME'] = str_replace ('アパマンショップ', '', $data['M05_TENPO_NAME']);

			// 無料電話番号
			$callData = $this->convertFreeCalling($data);
			$data['IS_FREE_CALL']  = $callData['IS_FREE_CALL'];
			$data['TEL_01']        = $callData['TEL_01'];
			$data['TEL_02']        = $callData['TEL_02'];
			$data['hasFreeTel']    = $callData['hasFreeTel'];
			$data['CONNECTION_NO'] = $callData['CONNECTION_NO'];

			// 営業時間変換
			$data['EIGYOU_ZIKAN'] = $this->convertEigyouZikan($data);
			
            // 定休日変換
    		$data['M05_TEIKYUUBI_NAME'] = $this->convertTeikyuubi($data);
			
			// 郵便番号変換
			$data['M05_TENPO_YUUBIN'] = substr($data['M05_TENPO_YUUBIN'], 0, 3) . '-' . substr($data['M05_TENPO_YUUBIN'], 3, 4);
			
			// 店舗外観写真
			$data['IMAGE_PATH'] = $tenpoimageurl . 'g' . Apm_ShopUtil::getOldTenpoCd($data['M05_TENPO_CD']) . '.jpg';

			// リンク先URL
			$data['SHOP_DTL_URL'] = $linkurl . '?ShopCd=' . $data['M05_TENPO_CD'] . '&' . substr($params, 1);
//			$data['SHOP_PAGE_URL'] = str_replace("shopdtl","shoppage",$linkurl) . '?ShopCd=' . $data['M05_TENPO_CD'] . '&' . substr($params, 1);
			
    		$dispData[] = $data;
    	}

    	return $dispData;
	}


    /**
     * 店舗近隣情報取得sql文生成
     * sql文生成
     * @param $params
     * @param $db_params
     * @return sql文
     */
	protected function generateSql($params, &$db_params)
    {
    	$strShopCd 		= $params['ShopCd'];
    	$dblX 			= $params['x'];
    	$dblY 			= $params['y'];
    	$intKinrinRange = $params['kinrin_range'];
    	
		// ダミー店舗除外
		$condKeyword = '';
		$condKeyword .= $this->generateNotinDummyShop("Z1.M05_TENPO_CD", $db_params);
		
$sql = <<< DOC_END

		select
	        Z1.M05_TENPO_CD
	        ,M05_HOUZIN_CD
	        ,M05_TENPO_NAME
	        ,M05_HOUZINMEI
	        ,M05_JYUUSHO_TEXT
	        ,M05_TENPO_BANCHI
	        ,M05_TENPO_JYUUSHO_BIKOU
	        ,M05_INFORMATION
	        ,M05_KAITENBI
	        ,M05_TENPO_TODOUFUKEN
	        ,M05_TENPO_SHIKUCHOUSON
	        ,M05_EIGYOU_ZIKAN_CLOSE_H
	        ,M05_EIGYOU_ZIKAN_CLOSE_M
	        ,M05_MIDDLE_FLAG
	        ,M05_TENPO_TEL01
	        ,M05_TENPO_TEL02
	        ,CASE
	            WHEN LPAD(NVL(M05_EIGYOU_ZIKAN_CLOSE_H, ''), 2, '0') || LPAD(NVL(M05_EIGYOU_ZIKAN_CLOSE_M, ''), 2, '0') > '1900' THEN 1
	            ELSE 0
	        END AS M05_EIGYOU_ZIKAN_CLOSE
	        ,M05_OEM
	        ,SUBSTR(SV.M05_SERVICE_FLAG, 12, 1) AS INFOMATION_CENTER_FLAG
	        ,M05_TENPO_YUUBIN
	        ,M05_KOUTSUU_TEXT01
	        ,M05_KOUTSUU_BIKOU01
	        ,M05_TOHO01
	        ,M05_TEIKYUUBI
	        ,M05_EIGYOU_ZIKAN_OPEN_H
	        ,M05_EIGYOU_ZIKAN_OPEN_M
	        ,M05_EIGYOU_ZIKAN_BIKOU
	        ,M05_EKI_CD01
	        ,M03_EKI_NAME
	        ,M05_TOKUCHO01
	        ,M05_TOKUCHO02
	        ,M05_TOKUCHO03
	        ,M05_TOKUCHO04
	        ,M05_TOKUCHO05
	        ,M05_TOKUCHO06
	        ,M05_TOKUCHO07
	        ,M05_TOKUCHO08
	        ,M05_TOKUCHO09
	        ,M05_TOKUCHO10
	        ,M05_TOKUCHO11
	        ,M05_TOKUCHO12
	        ,M05_TOKUCHO13
	        ,M05_TOKUCHO14
	        ,M05_TOKUCHO15
	        ,M05_TOKUCHO16
	        ,M05_ENSEN_CD01
	        ,M03_ENSEN_NAME
	        ,M02.M02_ENSEN_KUBUN
DOC_END;
for ($call_no=1; $call_no<=Apm_Config_Shop::CALLNOTE_NO_MAX; $call_no++) {
	$sql .= " ,M93_CONNECTION_NO".$call_no;
	$sql .= " ,M93_CONTROL_FLAG".$call_no;
}
$sql .= <<< DOC_END
	        ,M93_USE_KBN
	        ,M93_START_DATE
	        ,M93_DEL_DATE
		from
	        M05TENPO_INFO Z1
	        ,M05TENPO_SERVICE SV
	        ,M03EKI M03
	        ,M02ENSEN M02
	        ,M93CALLNOTE M93
		where
	        	Z1.M05_TENPO_CD = SV.M05_TENPO_CD
	        AND Z1.M05_EKI_CD01 = M03.M03_EKI_CD(+)
	        AND M03.M03_ENSEN_CD = M02.M02_ENSEN_CD(+)
	        AND Z1.M05_TENPO_CD = M93.M93_TENPO_CD(+)
			AND
		    sdo_within_distance(
		        Z1.M05_LOCATION,
		        SDO_GEOMETRY(
		            2001
		            ,8301
		            ,SDO_POINT_TYPE(
		                 {$dblX}
		                ,{$dblY}
		                ,NULL
		            )
		            ,NULL
		            ,NULL
		        ), 'distance={$intKinrinRange} unit=M'
		    ) = 'TRUE'
	        AND Z1.M05_KOUKAI_FLAG = 1
	        AND Z1.M05_TENPO_CD > '01000000'
	        AND Z1.M05_TENPO_CD < '48000000'
			AND Z1.M05_TENPO_CD <> '{$strShopCd}'
			{$condKeyword}

DOC_END;
	        
        return $sql;
    }

	/**
	 * 汎用コントロールテーブルから近隣店舗計算用設定を取得
	 *
	 * @param コントロールキー
	 * @return 設定内容（取得できなかった場合はnull）
	 */
	public function getControlSetting($control_key)
	{

$sql = <<< DOC_END

		SELECT
		    CTR_CD, CTR_NM, VAL1, VAL2, VAL3
		FROM
		    T97PROC_CONTROL
		WHERE
		    CTR_CD = '{$control_key}'
DOC_END;

		$settingDtl = $this->dbGetData($sql);

        // 設定が存在しない場合
        if (is_array($settingDtl) == false) return null;

		return $settingDtl;
	}


}

?>
